package com.extremelylargepackage.entity;

import java.io.Serializable;
import java.util.Map;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.Log;

import com.extremelylargepackage.StatTracker;
import com.extremelylargepackage.sprite.Animation;
import com.extremelylargepackage.sprite.CollidableSprite;

public abstract class Character extends Entity implements Serializable
{
	protected float _health;
	protected float _maxHealth;
	protected transient Weapon _weapon;

	public Character()
	{ 
		
	}
	
	public float getMaxHealth()
	{
		return _maxHealth;
	}

	public float getHealth()
	{
		return _health;
	}

	public void changeHealth(int healthMod)
	{
		_health += healthMod;
	}

	public void setMaxHealth(int newMaxHealth)
	{
		_maxHealth = newMaxHealth;
	}
	
	public Weapon getWeapon()
	{
		return _weapon;
	}

	@Override
	public void updateState(long time)
	{
		if(_status != Status.dead && _status != Status.dying && _health <= 0)
		{
			_status = Status.dying;
			if(_weapon != null)
				_weapon._status = Status.dead;
		} 
		if(_status == Status.dying) 
		{
			onDying(time);
		}

		if(_weapon != null)
			_weapon.updateState(time);
	}

	@Override
	public void draw(Canvas c)
	{
		if(_status != Status.dead && _status != Status.dying) 
		{
			c.drawBitmap(_sprite.getBitmap(), _locX, _locY, null); //???
			//drawHitbox(c);
		} else if(_explosionAnim != null && !_explosionAnim.finished()) 
		{
			drawExplosion(c);
		}
		if(_weapon != null)
			_weapon.draw(c);
	}
	
	private void drawHitbox(Canvas c)
	{
		if(_sprite instanceof CollidableSprite)
		{
			c.drawBitmap(((CollidableSprite)_sprite)._solid, _locX, _locY, null);
		}
	}

	/**
	 * Generates a map of values representing the state of the character. Ex { (health, 100), (x, 100), (y, 250) }
	 * @return state
	 */
	@Override
	public Map<String, Object> getState()
	{
		Map<String, Object> res = super.getState();

		res.put("health", String.valueOf(_health));
		res.put("maxHealth", String.valueOf(_maxHealth));
		if(_weapon != null)
			res.put("weapon", _weapon.getState());

		return res;
	}
}
